package com.example.eurekaserverclient.gclconverter;

import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.io.IOException;
import java.util.Arrays;

/**
 * @Author gcl
 * @Date 2020/7/2 11:01
 */
public class JackGclMessageConverter extends MappingJackson2HttpMessageConverter {
    public JackGclMessageConverter(){
        super();
        setSupportedMediaTypes(Arrays.asList(
                MediaType.APPLICATION_JSON,
                MediaType.TEXT_HTML,
                MediaType.TEXT_PLAIN
        ));
    }

    @Override
    protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        if (!(o instanceof UnifyResponse)){
            o = new UnifyResponse(o);
        }
        outputMessage.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        super.writeInternal(o, outputMessage);
    }
}
